package com.qymerge.manage.base.controller;

import com.google.common.reflect.TypeToken;
import com.qymerge.manage.base.dto.MetaOrgDTO;
import com.qymerge.manage.base.dto.OrganizationalStructureDTO;
import com.qymerge.manage.base.ro.MetaOrgRO;
import com.qymerge.manage.base.ro.MetaOrgRO;
import com.qymerge.manage.base.ro.MetaOrgVersionRO;
import com.qymerge.manage.base.service.IMetaOrgService;
import com.qymerge.manage.core.bean.*;
import com.qymerge.manage.core.constant.ParamKey;
import com.qymerge.manage.core.controller.BaseController;
import com.qymerge.manage.core.enums.StatusTypes;
import com.qymerge.manage.core.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>Title: IMetaOrgManageController</p>
 * <p>Description: 组织架构</p>
 *
 * @author Xiao
 * @date 2025/4/29 17:18
 * @version 1.0.0
 */
@Controller
@RequestMapping(value = "base/org")
public class IMetaOrgManageController extends BaseController {

    @Autowired
    private IMetaOrgService metaOrgService;

    /**
     * 列表
     *
     * @param parameters
     * @param callback
     */
    @RequestMapping("list")
    public void list(HttpServletRequest request, HttpServletResponse response,

                     @RequestParam(defaultValue = ParamKey.In.dlt_parameters) String parameters,
                     @RequestParam(defaultValue = ParamKey.In.empty) String callback) {
        ParamValue<MetaOrgRO> paramValue = GsonUtil.json2T(parameters, new TypeToken<ParamValue<MetaOrgRO>>() {
        }.getType());
        ParamCommon common = paramValue.getCommon();
        MetaOrgRO ro = paramValue.getData();

        DataTableRO data = metaOrgService.list(ro, new PageRequest(common.getPage(), common.getSize()));
        PrintCodeUtils.printSuccess(response, data.build(), callback);
    }

    /**
     * 列表
     *
     * @param parameters
     * @return
     */
    @RequestMapping(value = "/treeList")
    public void treeList(HttpServletRequest request, HttpServletResponse response,
                            @RequestParam(defaultValue = ParamKey.In.dlt_parameters) String parameters,
                            @RequestParam(defaultValue = ParamKey.In.empty) String callback) {
        ParamValue<MetaOrgRO> paramValue = GsonUtil.json2T(parameters, new TypeToken<ParamValue<MetaOrgRO>>() {
        }.getType());

        MetaOrgRO roSearch = paramValue.getData();
        List<MetaOrgRO> orgROList = metaOrgService.list(roSearch);

        // 封装到 MetaOrgDTO 中
        List<MetaOrgDTO> dtoList = new ArrayList<MetaOrgDTO>();
        if (!CollectionUtils.isEmpty(orgROList)) {
            for (MetaOrgRO ro : orgROList) {
                MetaOrgDTO dto = new MetaOrgDTO();
                dto.setId(CommonUtil.getLongNotNull(ro.getId()));
                dto.setParentId(CommonUtil.getLongNotNull(ro.getParentId()));
                dto.setName(CommonUtil.getStringNotNullValue(ro.getName()));
                dto.setFlag(CommonUtil.getIntegerNotNull(ro.getFlag()));
                dto.setNodeLevel(CommonUtil.getIntegerNotNull(ro.getNodeLevel()));
                dto.setOrgVersionId(CommonUtil.getLongNotNull(ro.getOrgVersionId()));
                dto.setSort(CommonUtil.getLongNotNull(ro.getSort()));
                dtoList.add(dto);
            }
        }

        PrintCodeUtils.printSuccess(response, MetaOrgUtils.formatMenuJson(dtoList), callback);
    }

    /**
     * 详情
     *
     * @param parameters
     * @param callback
     * @return
     */
    @RequestMapping(value = "/detail")
    public void detail(HttpServletRequest request, HttpServletResponse response,
                            @RequestParam(defaultValue = ParamKey.In.dlt_parameters) String parameters,
                            @RequestParam(defaultValue = ParamKey.In.empty) String callback) {
        ParamValue<MetaOrgRO> paramValue = GsonUtil.json2T(parameters, new TypeToken<ParamValue<MetaOrgRO>>() {
        }.getType());
        MetaOrgRO ro = paramValue.getData();

        MetaOrgRO result = metaOrgService.detail(ro.getId());
        if (null != result) {
            PrintCodeUtils.printSuccess(response, result, callback);
        } else {
            PrintCodeUtils.printError(response, "详情获取失败");
        }
    }

    /**
     * 新增
     *
     * @param parameters
     * @param callback
     * @return
     */
    @RequestMapping(value = "/save")
    public void saveSysMenu(HttpServletRequest request, HttpServletResponse response,

                            @RequestParam(defaultValue = ParamKey.In.dlt_parameters) String parameters,
                            @RequestParam(defaultValue = ParamKey.In.empty) String callback) {
        ParamValue<MetaOrgRO> paramValue = GsonUtil.json2T(parameters, new TypeToken<ParamValue<MetaOrgRO>>() {
        }.getType());
        MetaOrgRO ro = paramValue.getData();

        ActionResultRO data = metaOrgService.save(ro);
        PrintCodeUtils.printSuccess(response, data, callback);
    }

    /**
     * 修改
     *
     * @param parameters
     * @return
     */
    @RequestMapping(value = "/edit")
    public void editSysMenu(HttpServletRequest request, HttpServletResponse response,

                            @RequestParam(defaultValue = ParamKey.In.dlt_parameters) String parameters,
                            @RequestParam(defaultValue = ParamKey.In.empty) String callback) {
        ParamValue<MetaOrgRO> paramValue = GsonUtil.json2T(parameters, new TypeToken<ParamValue<MetaOrgRO>>() {
        }.getType());
        MetaOrgRO ro = paramValue.getData();

        ActionResultRO data = metaOrgService.edit(ro);
        PrintCodeUtils.printSuccess(response, data, callback);
    }

    /**
     * 删除
     *
     * @param parameters
     * @param callback
     * @return
     */
    @RequestMapping(value = "/del")
    public void delSysMenu(HttpServletRequest request, HttpServletResponse response,

                           @RequestParam(defaultValue = ParamKey.In.dlt_parameters) String parameters,
                           @RequestParam(defaultValue = ParamKey.In.empty) String callback) {
        ParamValue<MetaOrgRO> paramValue = GsonUtil.json2T(parameters, new TypeToken<ParamValue<MetaOrgRO>>() {
        }.getType());
        MetaOrgRO ro = paramValue.getData();

        ActionResultRO data = metaOrgService.delete(ro.getId());
        PrintCodeUtils.print(response, data, callback);
    }


}
